using System;
using Csla;
using Ent.AccesoDatos;
using System.Collections.Generic;
using System.Data;

namespace EntGuayasSLA.BL
{
    [Serializable]
    public class Promotores : BusinessBase<Promotores>
    {
        #region Business Methods
        public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(c => c.Id);
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }
        
        #region Datos de Persona
        public static readonly PropertyInfo<string> PersonaIdProperty = RegisterProperty<string>(c => c.PersonaId);
        public string PersonaId
        {
            get { return GetProperty(PersonaIdProperty); }
            set { SetProperty(PersonaIdProperty, value); }
        }
        public static readonly PropertyInfo<string> IdentificacionProperty = RegisterProperty<string>(c => c.Identificacion);
        public string Identificacion
        {
            get { return GetProperty(IdentificacionProperty); }
            set { SetProperty(IdentificacionProperty, value); }
        }

        public static readonly PropertyInfo<string> NombreProperty = RegisterProperty<string>(c => c.Nombre);
        public string Nombre
        {
            get { return GetProperty(NombreProperty); }
            set { SetProperty(NombreProperty, value); }
        }

        public static readonly PropertyInfo<string> ApellidoProperty = RegisterProperty<string>(c => c.Apellido);
        public string Apellido
        {
            get { return GetProperty(ApellidoProperty); }
            set { SetProperty(ApellidoProperty, value); }
        }

        public static readonly PropertyInfo<string> DireccionProperty = RegisterProperty<string>(c => c.Direccion);
        public string Direccion
        {
            get { return GetProperty(DireccionProperty); }
            set { SetProperty(DireccionProperty, value); }
        }

        public static readonly PropertyInfo<string> EmailProperty = RegisterProperty<string>(c => c.Email);
        public string Email
        {
            get { return GetProperty(EmailProperty); }
            set { SetProperty(EmailProperty, value); }
        }
        #endregion

        #region Datos de Promotor

        public static readonly PropertyInfo<string> RucProperty = RegisterProperty<string>(c => c.Ruc);
        public string Ruc
        {
            get { return GetProperty(RucProperty); }
            set { SetProperty(RucProperty, value); }
        }

        public static readonly PropertyInfo<string> RazonsocialProperty = RegisterProperty<string>(c => c.Razonsocial);
        public string Razonsocial
        {
            get { return GetProperty(RazonsocialProperty); }
            set { SetProperty(RazonsocialProperty, value); }
        }

        public static readonly PropertyInfo<string> RepresentanteLegalProperty = RegisterProperty<string>(c => c.RepresentanteLegal);
        public string RepresentanteLegal
        {
            get { return GetProperty(RepresentanteLegalProperty); }
            set { SetProperty(RepresentanteLegalProperty, value); }
        }

        public static readonly PropertyInfo<string> TelefonoProperty = RegisterProperty<string>(c => c.Telefono);
        public string Telefono
        {
            get { return GetProperty(TelefonoProperty); }
            set { SetProperty(TelefonoProperty, value); }
        }

        public static readonly PropertyInfo<string> TelefonoCelularProperty = RegisterProperty<string>(c => c.TelefonoCelular);
        public string TelefonoCelular
        {
            get { return GetProperty(TelefonoCelularProperty); }
            set { SetProperty(TelefonoCelularProperty, value); }
        }

        public static readonly PropertyInfo<string> FaxProperty = RegisterProperty<string>(c => c.Fax);
        public string Fax
        {
            get { return GetProperty(FaxProperty); }
            set { SetProperty(FaxProperty, value); }
        }
        public static readonly PropertyInfo<string> ParroquiaIdProperty = RegisterProperty<string>(c => c.ParroquiaId);
        public string ParroquiaId
        {
            get { return GetProperty(ParroquiaIdProperty); }
            set { SetProperty(ParroquiaIdProperty, value); }
        }
        public static readonly PropertyInfo<string> NacionalidadProperty = RegisterProperty<string>(c => c.Nacionalidad);
        public string Nacionalidad
        {
            get { return GetProperty(NacionalidadProperty); }
            set { SetProperty(NacionalidadProperty, value); }
        }
        public static readonly PropertyInfo<string> DelegadoProperty = RegisterProperty<string>(c => c.Delegado);
        public string Delegado
        {
            get { return GetProperty(DelegadoProperty); }
            set { SetProperty(DelegadoProperty, value); }
        }
        public static readonly PropertyInfo<string> TipoOrganizacionProperty = RegisterProperty<string>(c => c.TipoOrganizacion);
        public string TipoOrganizacion
        {
            get { return GetProperty(TipoOrganizacionProperty); }
            set { SetProperty(TipoOrganizacionProperty, value); }
        }
        #endregion
                
        #endregion

        #region Business Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //BusinessRules.AddRule(new Rule(), IdProperty);
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //BusinessRules.AddRule(...);
        }

        #endregion

        #region Factory Methods

        public static Promotores NewEditableRoot()
        {
            return DataPortal.Create<Promotores>();
        }
        /// <summary>
        /// Obtiene datos de promotor
        /// </summary>
        /// <param name="id">Id de promotor | Id de persona</param>
        /// <returns></returns>
        public static Promotores GetEditableRoot(Guid id)
        {
            return DataPortal.Fetch<Promotores>(id);
        }
        public static Promotores GetEditableRoot(string cedula)
        {
            return DataPortal.Fetch<Promotores>(cedula);
        }
        private Promotores()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access
         [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_pro_id", criteria.ToString()) };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_PROMOTOR\"", parametros);
            LoadPropertys(datos);
        }
        private void DataPortal_Fetch(string criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_pro_ruc", criteria) };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_PROMOTOR_CED\"", parametros);
            LoadPropertys(datos);
        }
        private void LoadPropertys(DataSet datos)
        {
            if (datos.Tables[0].Rows.Count > 0)
            {

                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0]["pro_id"].ToString()));
                LoadProperty(ParroquiaIdProperty, datos.Tables[0].Rows[0]["par_id"].ToString());
                LoadProperty(PersonaIdProperty, datos.Tables[0].Rows[0]["per_id"].ToString());
                LoadProperty(IdentificacionProperty, datos.Tables[0].Rows[0]["per_identificacion"].ToString());
                LoadProperty(NombreProperty, datos.Tables[0].Rows[0]["per_nombre"].ToString());
                LoadProperty(ApellidoProperty, datos.Tables[0].Rows[0]["per_apellido"].ToString());
                LoadProperty(DireccionProperty, datos.Tables[0].Rows[0]["per_direccion"].ToString());
                LoadProperty(EmailProperty, datos.Tables[0].Rows[0]["per_email"].ToString());
                LoadProperty(RucProperty, datos.Tables[0].Rows[0]["pro_ruc"].ToString().Trim());
                LoadProperty(RazonsocialProperty, datos.Tables[0].Rows[0]["pro_razon_social"].ToString());
                LoadProperty(RepresentanteLegalProperty, datos.Tables[0].Rows[0]["pro_representante_legal"].ToString());
                LoadProperty(TelefonoProperty, datos.Tables[0].Rows[0]["pro_telefono"].ToString());
                LoadProperty(TelefonoCelularProperty, datos.Tables[0].Rows[0]["pro_telefono_celular"].ToString());
                LoadProperty(FaxProperty, datos.Tables[0].Rows[0]["pro_fax"].ToString());
                LoadProperty(NacionalidadProperty, datos.Tables[0].Rows[0]["per_nacionalidad"].ToString());
                LoadProperty(DelegadoProperty, datos.Tables[0].Rows[0]["per_per_id"].ToString().Trim());
                LoadProperty(TipoOrganizacionProperty, datos.Tables[0].Rows[0]["pro_tipo_org"].ToString().Trim());

            }
        }
         
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Insert()
        {    var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_pro_id", ReadProperty(IdProperty)),
                new Parametro("ec_par_id", ReadProperty(ParroquiaIdProperty)),
                new Parametro("ec_per_identificacion", ReadProperty(IdentificacionProperty)),
                new Parametro("ec_per_nombre", ReadProperty(NombreProperty)),
                new Parametro("ec_per_apellido", ReadProperty(ApellidoProperty)),
                new Parametro("ec_per_direccion", ReadProperty(DireccionProperty)),
                new Parametro("ec_per_email", ReadProperty(EmailProperty)),

                new Parametro("ec_pro_ruc", ReadProperty(RucProperty)),
                new Parametro("ec_pro_razon_social", ReadProperty(RazonsocialProperty)),
                new Parametro("ec_pro_representante_legal", ReadProperty(RepresentanteLegalProperty)),
                new Parametro("ec_pro_telefono", ReadProperty(TelefonoProperty)),
                new Parametro("ec_pro_telefono_celular", ReadProperty(TelefonoCelularProperty)),
                new Parametro("ec_pro_fax", ReadProperty(FaxProperty)),
                new Parametro("ec_per_id", ReadProperty(PersonaIdProperty)),
                new Parametro("ec_per_nacionalidad", ReadProperty(NacionalidadProperty)),
                new Parametro("ec_per_per_id", ReadProperty(DelegadoProperty)),
                new Parametro("ec_pro_tipo_org", ReadProperty(TipoOrganizacionProperty))
            };
            accesoDatos.EjecutarQuery("\"INS_PROMOTOR\"", parametros);
     
        }
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Update()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_pro_id", ReadProperty(IdProperty)),
                new Parametro("ec_par_id", ReadProperty(ParroquiaIdProperty)),
                new Parametro("ec_per_identificacion", ReadProperty(IdentificacionProperty)),
                new Parametro("ec_per_nombre", ReadProperty(NombreProperty)),
                new Parametro("ec_per_apellido", ReadProperty(ApellidoProperty)),
                new Parametro("ec_per_direccion", ReadProperty(DireccionProperty)),
                new Parametro("ec_per_email", ReadProperty(EmailProperty)),
                new Parametro("ec_pro_ruc", ReadProperty(RucProperty)),
                new Parametro("ec_pro_razon_social", ReadProperty(RazonsocialProperty)),
                new Parametro("ec_pro_representante_legal", ReadProperty(RepresentanteLegalProperty)),
                new Parametro("ec_pro_telefono", ReadProperty(TelefonoProperty)),
                new Parametro("ec_pro_telefono_celular", ReadProperty(TelefonoCelularProperty)),
                new Parametro("ec_pro_fax", ReadProperty(FaxProperty)),
                new Parametro("ec_per_id", ReadProperty(PersonaIdProperty)),
                new Parametro("ec_per_nacionalidad", ReadProperty(NacionalidadProperty)),
                new Parametro("ec_per_per_id", ReadProperty(DelegadoProperty)),
                new Parametro("ec_pro_tipo_org", ReadProperty(TipoOrganizacionProperty))
            };
            accesoDatos.EjecutarQuery("\"ACT_PROMOTOR\"", parametros);
      
        }
         [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(this.Id);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_pro_id",ReadProperty(IdProperty)),
                new Parametro("ec_per_id",ReadProperty(PersonaIdProperty))
            };
            accesoDatos.EjecutarQuery("\"ELI_PROMOTOR\"", parametros);
        }



        #endregion

        #region Propiedades de Persona



        #endregion
    }
}
